﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using hzGis.model;
using ESRI.ArcGIS.Geometry;

namespace hzGis.utils
{
    public class GdbUtils
    {
        /// <summary>
        /// 在指定目录下创建gdb文件
        /// </summary>
        /// <param name="path">创建目录</param>
        /// <returns>返回工作空间</returns>
        public static FeatureClassInfo CreateFileGdbWorkspace(string gdbFileName)
        {
            Type factoryType = Type.GetTypeFromProgID(
                "esriDataSourcesGDB.FileGDBWorkspaceFactory");
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
                (factoryType);
            string path = System.IO.Path.GetDirectoryName(gdbFileName);
            string fileName = System.IO.Path.GetFileName(gdbFileName);

            IWorkspaceName workspaceName = workspaceFactory.Create(path, fileName, null, 0);
            IName name = (IName)workspaceName;
            IWorkspace workspace = (IWorkspace)name.Open();
            FeatureClassInfo fci = new FeatureClassInfo();
            fci.wpf = workspaceFactory;
            fci.wp = workspace;
            return fci;
        }

        public static IFeatureClass shpImportGdb(IFeatureClass sourceFeatureClass,List<FieldConn> sourceFelds,
            ISpatialReference prj, IWorkspace gdbWk, string tableName)
        {
            string shapeFielfName = "Shape";
            IFeatureWorkspace fw = gdbWk as IFeatureWorkspace;
            // 创建新的数据表
            IFields fields = new Fields();
            IFieldsEdit pFieldsEdit = fields as IFieldsEdit;
            for (int i = 0; i < sourceFelds.Count; i++) {
                IField targetField = new Field();
                IFieldEdit pFieldEdit = targetField as IFieldEdit;
                pFieldEdit.Name_2 = sourceFelds[i].targetField;
                pFieldEdit.Type_2 = sourceFelds[i].typeField;
                pFieldsEdit.AddField(targetField);
            }
            IField geometryField = new Field();
            IFieldEdit geometryFieldEdit = geometryField as IFieldEdit;
            geometryFieldEdit.Name_2 = shapeFielfName;
            geometryFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            IGeometryDef pGeometryDef = new GeometryDef();
            IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
            pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;  //此处可以是点、线、面
            geometryFieldEdit.GeometryDef_2 = pGeometryDef;
            pGeometryDefEdit.SpatialReference_2 = prj;
            pFieldsEdit.AddField(geometryField);
            // 这里直接创建gdb中的数据表
            IFeatureClass targetFeatureClass = fw.CreateFeatureClass(tableName, fields, null, null, esriFeatureType.esriFTSimple, shapeFielfName, "");
            // 直接遍历元数据
            IFeatureCursor sourceCursor = sourceFeatureClass.Search(null, false);
            IFeature feature = sourceCursor.NextFeature();
            while (feature != null)
            {
                IFeature targetFeature = targetFeatureClass.CreateFeature();
                IGeometry geometry = feature.ShapeCopy;
                geometry.Project(prj);
                targetFeature.Shape = geometry;
                for (int i = 0; i < sourceFelds.Count; i++)
                {
                    FieldConn fieldConn = sourceFelds[i];
                    int sourceNum = feature.Fields.FindField(fieldConn.sourceField);
                    if (sourceNum >= 0)
                    {
                        int targetNum = targetFeature.Fields.FindField(fieldConn.targetField);
                        if (targetNum >= 0)
                        {
                            object obj = feature.Value[sourceNum];
                            targetFeature.set_Value(targetNum, obj);
                        }
                    }
                }
                targetFeature.Store();
                feature = sourceCursor.NextFeature();
            }
            return targetFeatureClass;
        }
    }
}
